﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;


try
{
    const int port = 9000;
    var serverIp = "127.0.0.1";
    var serverEndpoint = new IPEndPoint(IPAddress.Parse(serverIp), port);
    var clientSocket = new UdpClient();

    var clientRequest = "Hello, Server!";
    var clientRequestBytes = Encoding.UTF8.GetBytes(clientRequest);

    clientSocket.Send(clientRequestBytes, clientRequestBytes.Length, serverEndpoint);
    Console.WriteLine($"已发送请求至服务器 {serverIp}:{port}：{clientRequest}");

    var serverResponseBytes = clientSocket.Receive(ref serverEndpoint);
    var serverResponse = Encoding.UTF8.GetString(serverResponseBytes);

    Console.WriteLine($"接收到来自服务器的响应：{serverResponse}");
    clientSocket.Close();
}
catch (Exception ex)
{
    Console.WriteLine($"发生异常：{ex.Message}");
}